/*
 *coder:tzl
 * date:3,11
 * intro:reading and writing to a array of stroct
 * serialization 模拟 
*/
#include<stdio.h>
#include<stdlib.h>

#define size 10
#define namesize 25

typedef struct
{
	char name[namesize];
	int midterm;
	int final;
	int homeworks;
}student;

void wirtestudentarray(char filename[],student a[],int n)
{
	FILE *fd;
	int i;
	if(n<=0)
	return;
	if((fd=fopen(filename,"w"))==NULL)
	{
		perror("fopen");
		exit(1);
	}
	for(i=0;i<n;i++)
	{
		fprintf(fd,"%s %d %d %d\n",a->name,a->midterm,a->final,a->homeworks);
		a++;
	}
	fclose(fd);
}

int readstudentarray(char filename[],student a[],int nmax)
{
	FILE *fd;
	int i=0;
	if((fd=fopen(filename,"r"))==NULL)
	{
		perror("fopen");
		exit(1);
	}
	while(fscanf(fd,"%s %d %d %d",a->name,&a->midterm,&a->final,&a->homeworks)!=EOF)
	{
		if(++i==nmax)break;
		a++;
	}
	fclose(fd);return i;
}

int main(int argc,char* argv[])
	{
		int n;
		student table[size];
		if(argc!=3)
		{
			printf("usage:%s infile outfile\n",argv[0]);
			exit(0);
		}
		n=readstudentarray(argv[1],table,size);
		wirtestudentarray(argv[2],table,n);
	}
